package com.letoken.platform.repo.app.redis;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Strings;
import com.letoken.platform.pub.constant.RedisKeyConstant;
import com.letoken.platform.pub.util.BeanUtil;
import com.letoken.platform.repo.admin.redis.AdminUserInfoRedis;
import com.letoken.platform.repo.admin.res.user.AdminUserInfoRes;
import com.letoken.platform.repo.app.po.AppUser;
import com.letoken.platform.repo.utlis.WebUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @version 1.0
 * @author： letokenlabs
 * @date： 2021/11/24 9:55
 * 用户当前登录token
 */
public class AppUserTokenRedis {
    private StringRedisTemplate stringRedisTemplate;

    private AppUserTokenRedis() {
        stringRedisTemplate = BeanUtil.getBean(StringRedisTemplate.class);
    }

    private enum Singleton {
        INSTANCE;
        private AppUserTokenRedis instance;

        // JVM会保证此方法绝对只调用一次
        Singleton() {
            instance = new AppUserTokenRedis();
        }
    }

    /**
     * 活动配置
     */
    String cacheKey = RedisKeyConstant.LETOKEN_APP_LOGIN_TOKEN;

    public static AppUserTokenRedis me() {
        return AppUserTokenRedis.Singleton.INSTANCE.instance;
    }

    public void put(String userId, String token) {
        if (!Strings.isNullOrEmpty(token)) {
            stringRedisTemplate.opsForHash().put(cacheKey, userId, token);
        }
    }

    public String getValue(String userId) {
        return (String) stringRedisTemplate.opsForHash().get(cacheKey, userId);
    }

    /**
     * @param key ... 多个key
     * @return
     */
    public Long del(String... key) {
        return stringRedisTemplate.opsForHash().delete(cacheKey, key);
    }
}
